//
//  WNProgressHUD.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit
import SnapKit
import MBProgressHUD

class WNHUD {
    
    static var root = appDelegate.wn_window!
    
    static var currentHUD: MBProgressHUD?
    
    /// 加载显示-加载中
    class func wn_showLoading(_ view: UIView = root, _ title: String = "") {
        if currentHUD != nil {
            currentHUD?.hide(animated: false)
            currentHUD = nil
        }
        currentHUD = MBProgressHUD.showAdded(to: view, animated: true)
        currentHUD?.backgroundView.style = .solidColor
        currentHUD?.backgroundView.color = UIColor(white: 0.0, alpha: 0.0)
        currentHUD?.label.text = title
        currentHUD?.label.textColor = .white
        currentHUD?.animationType = .zoom
        currentHUD?.mode = .indeterminate
        currentHUD?.bezelView.style = .solidColor
        currentHUD?.bezelView.color = .darkGray
    }
    
    /// 加载显示-动画
    class func wn_showLiveAnimateLoading(_ view: UIView = root, _ size: CGSize = .init(width: 50, height: 50)) {
        if currentHUD != nil {
            currentHUD?.hide(animated: false)
            currentHUD = nil
        }
        currentHUD = MBProgressHUD.showAdded(to: view, animated: true)
        let image = UIImageView.init(frame: CGRect(x: (view.width-size.width)/2, y: (view.height-size.height)/2, width: size.width, height: size.height))
        image.backgroundColor = .init(white: 0, alpha: 0.05)
        image.wn_cornerWithBorder(4)
        image.wn_animation(WNAnimation.wn_loadingArray, 1, 0)
        currentHUD?.mode = .customView
        currentHUD?.bezelView.style = .solidColor
        currentHUD?.bezelView.color = .gray
        currentHUD?.customView = image
//        currentHUD?.offset = CGPoint(x: 0, y: -86*(ScreenW/375.0))
        image.startAnimating()
    }
    
    class func wn_show(_ title: String, _ delayTime: TimeInterval = 1.5, _ view: UIView = root) {
        WNThread.wn_runInMainThread {
            if currentHUD != nil {
                currentHUD?.hide(animated: false)
                currentHUD = nil
            }
            currentHUD = MBProgressHUD.showAdded(to: view, animated: true)
            currentHUD?.mode = .text
            currentHUD?.backgroundView.style = .solidColor
            currentHUD?.backgroundView.color = UIColor(white: 0.0, alpha: 0.0)
            currentHUD?.label.text = title
            currentHUD?.label.textColor = .white
            currentHUD?.label.numberOfLines = 0
            currentHUD?.animationType = .zoom
            currentHUD?.bezelView.layer.cornerRadius = 4
            currentHUD?.bezelView.style = .solidColor
            currentHUD?.bezelView.color = .darkGray
            currentHUD?.hide(animated: true, afterDelay: delayTime)
            WNThread.wn_delay(delayTime) {
                currentHUD?.hide(animated: false)
                currentHUD = nil
            }
        }
    }
    
    /// 隐藏
    class func wn_hide() {
        DispatchQueue.main.async {
            if currentHUD != nil {
                currentHUD?.hide(animated: true)
                currentHUD = nil
            }
        }
    }
}
